package com.wwl.deliveryappointmentbackend.control.api;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.wwl.deliveryappointmentbackend.model.Deliver;
import com.wwl.deliveryappointmentbackend.service.DeliverService;
import com.wwl.deliveryappointmentbackend.utils.FileUtil;
import com.wwl.deliveryappointmentbackend.utils.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.web.servlet.MultipartProperties;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

@RestController
@RequestMapping("/api/user")
public class UserControl {

    @Value("${Constant.grant_type}")
    String grant_type;
    @Value("${Constant.secret}")
    String secret;
    @Value("${Constant.appid}")
    String appid;
    private final RestTemplate restTemplate;

    @Autowired
    public UserControl(RestTemplateBuilder restTemplateBuilder) {
        this.restTemplate = restTemplateBuilder.build();
    }

    @GetMapping("/getopenid")
    public Response<JSONObject> getopenid(@RequestParam String code) {
        String url ="https://api.weixin.qq.com/sns/jscode2session?js_code="+code+"&secret="+secret+"&grant_type="+grant_type+"&appid="+appid;
        String getopenid = restTemplate.getForObject(url,String.class);
        JSONObject jsonObject = JSONObject.parseObject(getopenid);
        return  Response.yes(jsonObject);
    }
}
